<?php
/*调用地址：http://域名/wechat.php*/
namespace Home\Controller;
use Think\Controller;
use Org\Wechat\api;
use Org\Wechat\message;
//define your token
define("TOKEN", "thinkwechat");
class IndexController extends Controller {
    //微信主入口函数
    public function index(){    
        //$this->responseMsg();//调用微信接口进行应答 
        $this->valid();//微信验证
    }
    //验证函数
    public function valid(){
        //valid signature , option
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
          echo $echoStr;
          exit;
        }
    }
    public function responseMsg(){
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    //extract post data
    if (!empty($postStr)){  
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        	//将获取的数据封装到message类中
            $msg = new message($postObj);      
            $api = new api($msg);
            switch($msg->msgTyple){
             //文本信息处理
             case "text":{
                          $resultStr = $api->Text_deal();
                          echo $resultStr;
                         }break;              
             //处理地理位置信息
             case "location":{
                          $resultStr = $api->Location_deal();
                          echo $resultStr;
                        }break; 
             //处理事件
             case "event":{
                          $resultStr = $api->Event_deal();
                          echo $resultStr;
                      }break;
              //图片信息
              case "image":{
                          $resultStr = $api->Image_deal();
                          echo $resultStr;
                          }break;
              case "voice":{
                          $resultStr = $api->Voice_deal();
                          echo $resultStr;
                          }break;  
              case "video":{
                          $resultStr = $api->Video_deal();
                          echo $resultStr;
                          }break;
              case "shortvideo":{
                          $resultStr = $api->ShortVideo_deal();
                          echo $resultStr;
                          }break;                             
             default:echo "Input something...";break;
            }      
        }else {
          echo "";
          exit;
        }
    } 
  private function checkSignature(){
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];    
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    if( $tmpStr == $signature ){
      return true;
    }else{
      return false;
    }
  }
}
?>